package com.pai4j.client.controller;
import com.pai4j.client.service.AccountRelationService;
import com.pai4j.common.bean.PAIResponseBean;
import com.pai4j.common.bean.PAIResponseBeanUtil;
import com.pai4j.common.helper.SessionHelper;
import com.pai4j.domain.vo.request.CreateGroupRequestVO;
import com.pai4j.domain.vo.response.AccountRelationResponseVO;
import com.pai4j.domain.vo.response.WaitConfirmUserResponseVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/client-service/chat/account-relation")
public class AccountRelationController {


    @Autowired
    private AccountRelationService accountRelationService;

    /**
     * 取消订阅/关注
     *
     * @param account
     * @return
     */
    @PostMapping("/subscribe/{account}")
    public PAIResponseBean<Boolean> subscribe(@PathVariable String account) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(accountRelationService.subscribe(userId, account));
    }

    /**
     * 取消订阅/关注
     *
     * @param account
     * @return
     */
    @PostMapping("/unsubscribe/{account}")
    public PAIResponseBean<Boolean> unsubscribe(@PathVariable String account) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(accountRelationService.unsubscribe(userId, account));
    }

    /**
     * 同意好友关系
     *
     * @param relationId
     * @return
     */
    @PostMapping("/confirmSubscribe/{relationId}")
    public PAIResponseBean<Boolean> confirmSubscribe(@PathVariable Long relationId, @PathVariable String remark) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(accountRelationService.confirmSubscribe(userId, relationId, remark));
    }

    /**
     * 拒绝好友
     *
     * @param relationId
     * @return
     */
    @PostMapping("/rejectSubscribe/{relationId}")
    public PAIResponseBean<Boolean> rejectSubscribe(@PathVariable Long relationId, @PathVariable String remark) {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(accountRelationService.rejectSubscribe(userId, relationId,remark));
    }

    /**
     * 加载关系账号：关注用户/机器人/群组/公众号
     * @return
     */
    @GetMapping("/list")
    public PAIResponseBean<AccountRelationResponseVO> loadAccountRelations() {
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(accountRelationService.loadAccountRelations(userId));
    }

    /**
     * 查询用户待审核的关系列表
     *
     * @return
     */
    @GetMapping("/wait-confirm/list")
    public PAIResponseBean<List<WaitConfirmUserResponseVO>> listWaitConformUsers() {
        // 获取当前登录用户
        String userId = SessionHelper.getCurrentUserId();
        return PAIResponseBeanUtil.success(accountRelationService.listWaitConformUsers(userId));
    }


    /**
     * 创建群聊
     *
     * @param createGroupRequest
     * @return
     */
    @PostMapping("/createGroup")
    public PAIResponseBean<String> createChatGroup(@RequestBody CreateGroupRequestVO createGroupRequest) {
        Assert.notNull(createGroupRequest, "参数为空");
        Set<String> userIds = createGroupRequest.getUserIds();
        Assert.isTrue(CollectionUtils.isNotEmpty(createGroupRequest.getUserIds()) &&
                userIds.size() >= 3 && userIds.size() <= 500, "群成员3～500人");
        // 设置群聊创建人、默认群主
        String userId = SessionHelper.getCurrentUserId();
        createGroupRequest.setCreateUserId(userId);
        return PAIResponseBeanUtil.success(accountRelationService.createChatGroup(createGroupRequest));
    }
}
